//////////////////////////////////////////////////////////////////////////////
//
//  Created by Philip Mulcahy on 01/03/2013.
//  Copyright (c) 2013 Philip Mulcahy. All rights reserved.
//
//  This file is part of the note-recog library.
//
//  note-recog is free software: you can redistribute it and/or modify
//  it under the terms of version 3 of the GNU Lesser General Public License 
//  as published by the Free Software Foundation.
//
//  note-recog is distributed in the hope that it will be useful,
//  but WITHOUT ANY WARRANTY; without even the implied warranty of
//  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//  GNU General Public License for more details.
//
//  You should have received a copy of the GNU General Public License
//  along with note-recog. If not, see <http://www.gnu.org/licenses/>.
//
//////////////////////////////////////////////////////////////////////////////

#include "ReplayFrameGenerator.h"
#include "Frame.h"
#include "SequenceIO.h"

template<typename T>
std::deque<typename T::value_type> MakeDeque(const T& t)
{
    return std::deque<typename T::value_type>(t.begin(), t.end());
}

ReplayFrameGenerator::ReplayFrameGenerator(const char* file_path, float sampling_freq, size_t frame_size)
: _sampling_freq(sampling_freq),
  _time_domain(MakeDeque(SequenceIO::readToVector<short>(file_path))),
  _frame_size(frame_size),
  _next_timestamp(0.f)
{}

ReplayFrameGenerator::~ReplayFrameGenerator(){}

Frame ReplayFrameGenerator::next()
{
    if(!hasNext())
    {
        throw std::logic_error("Nothing left in the tank: no more time domain data left.");
    }
    const auto begin = _time_domain.begin();
    const auto end = begin + _frame_size;
    Frame result(std::vector<short>(begin, end), _next_timestamp, _sampling_freq);
    _time_domain.erase(begin, end);
    _next_timestamp += _frame_size / _sampling_freq;
    return result;
}

bool ReplayFrameGenerator::hasNext() const
{
    return _time_domain.size() >= _frame_size;
}
